package com.xxgc.vfc.common.error.center.aop;

import com.xxgc.vfc.api.result.Result;
import com.xxgc.vfc.common.error.center.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/*
 *
 *   @Author: Sjy
 *   @Date: 2023/11/8-11-08-11:03
 *   用于捕获异常
 */
@Slf4j
@Aspect
@Component
public class ErrorAOP {


    //请求返回之后拦截
    @Around("execution(* *..*.controller.*Controller.*(..))")
    public Object afterErrorAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("--------------------拦截---------------------------");
        //获取相应结果
        log.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        Result result = (Result) proceedingJoinPoint.proceed();
        log.info("333332222222222222222222222222");
        log.info("断言"+(result.getData() instanceof BaseException));
        //如果响应结果的data是BaseException
        if (result.getData() instanceof BaseException) {
            //获取servlet的attributes
            ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
            //拿到request
            HttpServletRequest request = attributes.getRequest();
            //拿到请求路径
            String url = request.getRequestURI().toString();
            log.info("当前请求路径是"+url);
            BaseException be = (BaseException) result.getData();
            be.setInterfaceInfo(url);
            Signature signature = proceedingJoinPoint.getSignature();
            // 方法名
            be.setMethodInfo(signature.getDeclaringTypeName() + "." + signature.getName());
        }
        return result;
    }

}
